package com.jc.service.yzpService;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.ExceptionUtil;
import com.jc.common.util.HttpUtil;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.*;
import com.jc.framework.enums.*;
import com.jc.service.api.ProductCheckAdapter;
import com.jc.service.h5.ShopService;
import com.jc.service.model.h5.H5RespModel;
import com.jc.service.supplier.SupplierService;
import com.jc.service.suppliergoods.SupplierGoodsService;
import com.jc.service.suppliergoodspicturelist.SupplierGoodsPictureListService;
import com.jc.service.supplierproduct.SupplierProductService;
import com.jc.service.supplierproductprice.SupplierProductPriceService;
import com.jc.service.systemloglist.SystemLogListService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@SuppressWarnings("all")
@Service
public class YZPProductServiceImpl extends ProductCheckAdapter implements YZPProductService {

    @Autowired
    private SystemLogListService systemLogListService;
    @Value("${yzp.url}")
    private String url;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private SupplierGoodsService supplierGoodsService;
    @Autowired
    private SupplierGoodsPictureListService supplierGoodsPictureListService;
    @Autowired
    private SupplierProductPriceService supplierProductPriceService;
    @Autowired
    private SupplierProductService supplierProductService;
    @Autowired
    private ShopService shopService;


    /***
     *查询优质牌商品
     */

    @Transactional
    @Override
    public Map<String, Object> queryYzpProductList(Map<String, Object> param) {

//        if (param.get("pageNo") == null || "".equals(param.get("pageNo"))) {
//            throw new BaseServiceException("缺少必要参数：需要的当前页码");
//        }
        if (param.get("appSecret") == null || "".equals(param.get("appSecret"))) {
            throw new BaseServiceException("缺少秘钥");
        }
        Map<String, Object> map = null;
        String result = null;
        try {
            result = HttpUtil.get(url + YzpApi.PRODUCT_LIST_SELECT.getName(), param);
//              result = HttpUtil.get("http://api.youzhipai.cn/api/productList", param);
//            result = "{\"totalRecords\":15,\"code\":200,\"data\":[{\"quantity\":100,\"productNormName\":\"专票规格2\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1160,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"zpsp2.0000117\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":100,\"productNormName\":\"专票规格5\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1161,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"zpsp5.0000120\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":100,\"productNormName\":\"专票规格3\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1162,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"zpsp3.0000118\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":10000,\"productNormName\":\"卡灰规格1\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg\",\"productItemId\":1138,\"description\":\"香醇味浓的黑卡灰/dog\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg!sellout\",\"productNum\":\"kh1.0000127\",\"normTypeName\":\"规格\",\"productName\":\"黑卡灰\"},{\"quantity\":100,\"productNormName\":\"卡灰规格3\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1164,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"kh3.0000129\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":100,\"productNormName\":\"卡灰规格5\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1165,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"kh5.0000131\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":12000,\"productNormName\":\"卡灰规格2\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg\",\"productItemId\":1135,\"description\":\"香醇味浓的黑卡灰/dog\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg!sellout\",\"productNum\":\"kh2.0000128\",\"normTypeName\":\"规格\",\"productName\":\"黑卡灰\"},{\"quantity\":94,\"productNormName\":\"卡灰规格4\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1167,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"kh4.0000130\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":0,\"productNormName\":\"卡飞规格\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1531208290804.jpeg!sellout!watermark\",\"productItemId\":1096,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1531208290804.jpeg!sellout\",\"productNum\":\"kf.0000132\",\"normTypeName\":\"单位\",\"productName\":\"卡飞\"},{\"quantity\":23,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productItemId\":1129,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productNum\":\"zl.0000217\",\"normTypeName\":\"个\",\"productName\":\"中粮15.16.17\"},{\"quantity\":121,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productItemId\":1128,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productNum\":\"zl.0000215\",\"normTypeName\":\"个\",\"productName\":\"中粮15.16.17\"},{\"quantity\":456,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productItemId\":1130,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productNum\":\"zl.0000216\",\"normTypeName\":\"个\",\"productName\":\"中粮15.16.17\"},{\"quantity\":57,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1543895153452.jpeg\",\"productItemId\":1143,\"description\":\"海外商品 分销\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1543895153452.jpeg\",\"productNum\":\"haiwai001\",\"normTypeName\":\"个\",\"productName\":\"海外商品 分销\"},{\"quantity\":111,\"productNormName\":\"1个\",\"price\":2.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productItemId\":1144,\"description\":\"海外商品4\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productNum\":\"hwsp004\",\"normTypeName\":\"个\",\"productName\":\"海外商品4\"},{\"quantity\":9997,\"productNormName\":\"1个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productItemId\":1146,\"description\":\"海外商品4\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productNum\":\"hwbxy\",\"normTypeName\":\"个\",\"productName\":\"物料海外\"}],\"status\":\"success\"}";
            systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.OK, null, LogType.PRODUCT_LIST_SELECT, result);
        } catch (Exception e) {
            systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 150), LogType.PRODUCT_LIST_SELECT, result);
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(result)) {
            throw new BaseServiceException("请求的结果为空");
        }

        Supplier supplier = new Supplier();
        supplier.setFullName("分销商品");
        List<Supplier> suppliers = this.supplierService.queryAll(supplier);// todo 判空
        if (suppliers == null) {
            throw new BaseServiceException("对应的供应商不存在" + "分销商品");
        }
        String supplierSn = suppliers.get(0).getSupplierSn();//供应商的id
        System.out.println(supplierSn);


        //  System.out.println(result);
        //把查询到的分销商品数据返回
        map = JSONObject.parseObject(result, Map.class);
        //获取到商品数据
        //   Map<String, Object> goodsDetail = JSONObject.parseObject(map.get("data").toString(), Map.class);
        List data = (List) map.get("data"); // todo List<Map>
        if (data == null) {
            throw new BaseServiceException("获取数据失败原因" + JSONObject.toJSONString(data));
        }
        for (Object datum : data) {
            Map<String, Object> goodsDetail = JSONObject.parseObject(datum.toString(), Map.class);


            //保存商品
            SupplierGoods supplierGoods = packGoods(goodsDetail);
            SupplierGoods supplierGoodsQuer = new SupplierGoods();
            supplierGoodsQuer.setSupplierGoodsId(supplierGoods.getSupplierGoodsId());

            List<SupplierGoods> supplierGoods1 = this.supplierGoodsService.queryAll(supplierGoodsQuer);

            //商品存在同步遍历比较更新库存和价格
            if (CollectionUtils.isNotEmpty(supplierGoods1)) {
                //SupplierGoods的商品编码查询
                for (SupplierGoods goods : supplierGoods1) {
                    String supplierGoodsId = goods.getSupplierGoodsId();


                    SupplierProduct supplierProduct = new SupplierProduct();
                    supplierProduct.setProductId(supplierGoodsId); // todo +条件 分销商品  or   gid
                    supplierProduct.setGid(goods.getId());
                    List<SupplierProduct> supplierProducts = this.supplierProductService.queryAll(supplierProduct);
                    //对比不相等跟新库存
                    for (SupplierProduct product : supplierProducts) {
                        if (!product.getInventoryNum().equals(goodsDetail.get("quantity"))) {

                            SupplierProduct supplierProduct1 = new SupplierProduct();
                            supplierProduct1.setInventoryNum((Integer) goodsDetail.get("quantity"));
                            supplierProduct1.setProductId(supplierGoodsId);
                            supplierProduct1.setGid(goods.getId());
                            supplierProduct1.setId(product.getId());

                            int modify = this.supplierProductService.modify(supplierProduct1, false);//TODO
                            //推送到商城 // todo skuid 用sku主键
                            shopService.pushInventory(Long.valueOf(supplierProduct1.getId()), String.valueOf(goodsDetail.get("quantity")));
                        }


                        if (goodsDetail.get("price") != null) {


                            //更新价格
                            String price = String.valueOf(goodsDetail.get("price"));
                            Integer goodPrice = 0;
                            if (price.contains(".")) {
                                Double i = Double.valueOf(price) * 100;
                                goodPrice = Integer.valueOf(String.valueOf(i).split("\\.")[0]);
                            } else {
                                goodPrice = Integer.valueOf(price) * 100;
                            }
                            // TODO 暂定进价85折
                            Integer inGoodPrice = (goodPrice * 85) / 100;
                            SupplierProductPrice supplierProductPrice1 = new SupplierProductPrice();
                            supplierProductPrice1.setSupplierProductId(Long.valueOf(product.getId()));
                            List<SupplierProductPrice> supplierProductPrices1 = this.supplierProductPriceService.queryAll(supplierProductPrice1);
                            for (SupplierProductPrice supplierProductPrice : supplierProductPrices1) {


                                if (supplierProductPrice.getCooperateprice() != goodPrice) {


                                    SupplierProductPrice supplierProductPrice12 = priceCalculation(0, 0, inGoodPrice, goodPrice, 0, Long.valueOf(product.getId()));
                                    supplierProductPrice12.setId(supplierProductPrice.getId());
                                    int modify = this.supplierProductPriceService.modify(supplierProductPrice12, false);
                                    //推送到商城
                                    H5RespModel h5RespModel = shopService.pushH5(ThirdPartyParam.UPDATE_PRO_H5.getName(), new HashMap<String, Object>() {{
                                        put("skuCode", product.getId());
                                        put("price", supplierProductPrice.getCostPrice());
                                        put("taobaoPrice", supplierProductPrice.getSaleprice());
                                        put("commercePrice", supplierProductPrice.getLeve1Price());
                                        put("elitesPrice", supplierProductPrice.getLeve2Price());
                                        put("crownPrice", supplierProductPrice.getLeve3Price());
                                        put("supremacyPrice", supplierProductPrice.getLeve4Price());
                                        put("cost", supplierProductPrice.getUnionCostPrice());
                                        put("parentCost", supplierProductPrice.getCostPrice());
                                    }}, LogType.PRO_PRICE_AND_NUM_YZP_PUSH_SHOP);

                                }


                            }


                        }
                    }
                }
                continue;
            }

//            System.out.println("======================================");
//            System.out.println(supplierGoods);
//            System.out.println("======================================");

            supplierGoodsService.add(supplierGoods, false);

            //保存图片 //todo 大图确认是否为详情图 是否有顺序
            String firstImage = goodsDetail.get("firstImage").toString();
//            List<String> largeImageUrl = JSONObject.parseObject(goodsDetail.get("largeImageUrl").toString(), List.class);
            String largeImageUrl1 = (String) goodsDetail.get("largeImageUrl");
            String[] split = largeImageUrl1.split(",");
            ArrayList<String> largeImageUrl = new ArrayList<>();
            for (String s1 : split) {
                largeImageUrl.add(s1);
            }
            largeImageUrl.add(firstImage);
            largeImageUrl.forEach(image -> {
                        SupplierGoodsPictureList supplierGoodsPictureList = new SupplierGoodsPictureList();
                        supplierGoodsPictureList.setImagePath(image);
                        supplierGoodsPictureList.setWidth(200);
                        supplierGoodsPictureList.setHeight(200);
                        supplierGoodsPictureList.setImageType(1);
                        supplierGoodsPictureList.setImageOrder(0);
                        supplierGoodsPictureList.setGoodsId(supplierGoods.getSupplierGoodsId());
                        supplierGoodsPictureList.setGid(supplierGoods.getId());
                        this.supplierGoodsPictureListService.add(supplierGoodsPictureList, false);
                    }
            );
            largeImageUrl.forEach(image -> {
                        SupplierGoodsPictureList supplierGoodsPictureList = new SupplierGoodsPictureList();
                        supplierGoodsPictureList.setImagePath(image);
                        supplierGoodsPictureList.setWidth(200);
                        supplierGoodsPictureList.setHeight(200);
                        supplierGoodsPictureList.setImageType(2);
                        supplierGoodsPictureList.setImageOrder(0);
                        supplierGoodsPictureList.setGoodsId(supplierGoods.getSupplierGoodsId());
                        supplierGoodsPictureList.setGid(supplierGoods.getId());
                        this.supplierGoodsPictureListService.add(supplierGoodsPictureList, false);
                    }
            );
            //supplier_product
            SupplierProduct supplierProduct = new SupplierProduct();
            supplierProduct.setGid(supplierGoods.getId());
            supplierProduct.setProductId(String.valueOf(supplierGoods.getSupplierGoodsId()));
            supplierProduct.setProductName(String.valueOf(goodsDetail.get("productName")));
            supplierProduct.setProductImagePath(largeImageUrl.get(0));
            supplierProduct.setHastax(0);
            supplierProduct.setInventoryNum(Integer.valueOf(String.valueOf(goodsDetail.get("quantity"))));
            supplierProduct.setGoodsId(String.valueOf(goodsDetail.get("productNum")));
            /**
             * 0 : 不推送
             1 : 待推送
             2 : 已推送
             3 : 推送失败
             */
            supplierProduct.setPushStatus(Push.WAIT_PUSH.getName());
            /**
             * 是否进行图片压缩 0 未压缩 1 已经压缩
             * */
            supplierProduct.setIsImgCompression(ImageStatus.UNCOMPRESSED.getName());
            //规格
            String normTypeName = (String) goodsDetail.get("normTypeName");//黑卡灰
            String productNormName = (String) goodsDetail.get("productNormName");//
            Map<String, String> speMap = new HashMap<>();
            speMap.put("productNormName", productNormName);
            speMap.put("normTypeName", normTypeName);
            ArrayList<Object> spe = new ArrayList<>();
            spe.add(speMap);
            String specifications_string = JSONObject.toJSONString(spe);
            supplierProduct.setSpecificationsString(specifications_string);
            this.supplierProductService.add(supplierProduct, false);


            //保存价格
            //保存pro和价格的关系
            if (goodsDetail.get("price") != null) {
                String price = String.valueOf(goodsDetail.get("price"));
                Integer goodPrice = 0;
                if (price.contains(".")) {
                    Double i = Double.valueOf(price) * 100;
                    goodPrice = Integer.valueOf(String.valueOf(i).split("\\.")[0]);
                } else {
                    goodPrice = Integer.valueOf(price) * 100;
                }
                // TODO 暂定进价85折
                Integer inGoodPrice = (goodPrice * 85) / 100;

                SupplierProductPrice supplierProductPrice = priceCalculation(0, 0, inGoodPrice, goodPrice, 0, Long.valueOf(supplierProduct.getProductId()));
                SupplierProductPrice supplierProductPriceQu = new SupplierProductPrice();
                List<SupplierProduct> supplierProducts = this.supplierProductService.queryAll(supplierProduct);
                for (SupplierProduct product : supplierProducts) {


                    supplierProductPrice.setSupplierProductId(product.getId());
                    this.supplierProductPriceService.add(supplierProductPrice, false);

                }
            }


        }

        return map;
    }


    /**
     * 接收到的商品封装到supplierGoods
     */

    private SupplierGoods packGoods(Map<String, Object> param) {
        SupplierGoods supplierGoods = new SupplierGoods();
        //产品名字
        supplierGoods.setName(param.get("productName").toString());
        //品牌id
        supplierGoods.setBrandid(Long.valueOf(param.get("productItemId").toString()));
        //品牌中文
        supplierGoods.setBrandnamecn(param.get("productName").toString());
        //品牌英文
        supplierGoods.setBrandnameen(null);
        //TODO 一级分类暂时为空
        supplierGoods.setTypeId(null);
        supplierGoods.setTypeName(null);
        //二级分类 TODO
        supplierGoods.setCategoryParentId(null);
        supplierGoods.setCategoryParentName(null);
        //三级分类 TODO
        supplierGoods.setCategoryId(null);
        supplierGoods.setCategoryName(null);
        //产品详情格式类型 html , pic
        supplierGoods.setGoodsDescType("pic");
        // 这个字段 存储产品介绍详情，如果是图片不从这里取值 ，这里面只存富文本类型
        supplierGoods.setGoodsDesc(null);
        // todo 供应商系统产品ID，用文本类型为了兼容不同供应商系统 todo
        supplierGoods.setSupplierGoodsId(param.get("productItemId").toString());
        supplierGoods.setGoodNatureType(1);
        // todo 供应商ID
        Supplier supplier = new Supplier();
        supplier.setFullName("分销商品");
        List<Supplier> suppliers = supplierService.queryAll(supplier);
        if (CollectionUtils.isEmpty(suppliers)) {
            throw new BaseServiceException("当前产品查询不到对应的供应商[" + supplier.getFullName() + "],产品id为:" + param.get("id"));
        }
        supplierGoods.setSupplierId(Integer.valueOf(suppliers.get(0).getSupplierSn().toString()));
        //供应商名称
        supplierGoods.setSupplierName(suppliers.get(0).getFullName());
        //同步时间
        supplierGoods.setCreateTime(new Date());
        //最后更新时间
        supplierGoods.setLastUpdateTime(new Date());
        supplierGoods.setGoodNatureType(1);
        supplierGoods.setGoodsDesc((String) param.get("description"));

        // todo 规格描述
        /***
         * [{"name":"排量","value":[{"detailName":"4.0T"}]},{"name":"颜色","value":[{"detailName":"红色"}]}]
         *
         */
        Map<String, Object> nameMap1 = new HashMap<>();
        nameMap1.put("name", "规格");
//        Map<String, Object> nameMap2 = new HashMap<>();
//        nameMap2.put("name", "商品描述");
        Map<String, Object> nameMap3 = new HashMap<>();
        nameMap3.put("name", "规格值");


        ArrayList<Object> valueList1 = new ArrayList<>();
        Map<String, Object> valueMap1 = new HashMap<>();
        valueMap1.put("normTypeName", (String) param.get("normTypeName"));
        valueList1.add(valueMap1);
        nameMap1.put("value", valueList1);

//        ArrayList<Object> valueList2 = new ArrayList<>();
//        Map<String, Object> valueMap2 = new HashMap<>();
//        valueMap2.put("description", (String) param.get("description"));
//        valueList2.add(valueMap2);
//        nameMap2.put("value", valueList2);

        ArrayList<Object> valueList3 = new ArrayList<>();
        Map<String, Object> valueMap3 = new HashMap<>();
        valueMap3.put("productNormName", (String) param.get("productNormName"));
        valueList3.add(valueMap3);
        nameMap3.put("value", valueList3);

        ArrayList<Object> specifications_string = new ArrayList<>();
        specifications_string.add(nameMap1);
//        specifications_string.add(nameMap2);
        specifications_string.add(nameMap3);


        String s = JSONObject.toJSONString(specifications_string);
        supplierGoods.setSpecificationsString(s);

        return supplierGoods;
    }


    /*  * 计算价格
     * hastax 是否含税
     * taxCooperatePrice 含税价
     * cooperatePrice 不含税价
     * salePrice 市场价
     * defaultFreight 运费 默认为0*/


    public SupplierProductPrice priceCalculation(Integer hastax, Integer taxCooperatePrice, Integer cooperatePrice, Integer salePrice, Integer freight, Long productId) {


        //0 不含税 1 含税
        SupplierProductPrice _supplierProductPrice = new SupplierProductPrice();

        //含税价 分
        _supplierProductPrice.setTaxcooperateprice(taxCooperatePrice);
        //不含税价 分
        _supplierProductPrice.setCooperateprice(cooperatePrice);
        //运费
        if (freight == null) {
            freight = ProductPrice.DEFAULTSHIPPINGCOST.getName();
        }
        //成本价
        if (0 == hastax) {
            _supplierProductPrice.setCostPrice(cooperatePrice + freight);
        } else {
            _supplierProductPrice.setCostPrice(taxCooperatePrice + freight);
        }
        //市场价是否为空
        if (salePrice == null) {
            _supplierProductPrice.setSaleprice(_supplierProductPrice.getCostPrice() + (1 * ProductPrice.MARKETPRICEPERCENTAGE.getName() / 100));
        } else {
            _supplierProductPrice.setSaleprice(salePrice);
        }
        int private_cost_price = 0;
        if (hastax == 0) {// 不含税
            private_cost_price = _supplierProductPrice.getCooperateprice() + freight;
        } else {
            private_cost_price = _supplierProductPrice.getTaxcooperateprice() + freight;
        }
        // 求差价
        int diffPrice = (_supplierProductPrice.getSaleprice() + freight) - private_cost_price;
        if (diffPrice < 0) {
            System.out.println("严重警告 ,出现负数................................... 产品SKU ID : " + productId);
        }
        int step = diffPrice / 4; // 四个等级，每级的差价
        _supplierProductPrice.setSupplierProductId(Long.valueOf(productId));
        _supplierProductPrice.setMoneyPrice(_supplierProductPrice.getSaleprice());
        _supplierProductPrice.setLeve1Price(private_cost_price + step + step + step);
        _supplierProductPrice.setLeve2Price(private_cost_price + step + step);
        _supplierProductPrice.setLeve3Price(private_cost_price + step);
        _supplierProductPrice.setLeve4Price(private_cost_price);
        _supplierProductPrice.setUnionCostPrice(private_cost_price);
        _supplierProductPrice.setDefaultFreight(freight);
        return _supplierProductPrice;
    }


    /**
     * todo 同步分销商品的价格库存
     */
    @Transactional
    @Override
    public Boolean updateProducts(Map<String, Object> params) {
        Map<String, Object> resultMap = null;

        String str = null;
        try {
            str = HttpUtil.get(url + YzpApi.PRODUCT_LIST_SELECT.getName(), params);
//            str = HttpUtil.get(url + YzpApi.PRODUCT_LIST_SELECT.getName(), params);
//            str = "{\"totalRecords\":15,\"code\":200,\"data\":[{\"quantity\":333,\"productNormName\":\"专票规格2\",\"price\":2.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1160,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"zpsp2.0000117\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":100,\"productNormName\":\"专票规格5\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1161,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"zpsp5.0000120\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":100,\"productNormName\":\"专票规格3\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1162,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"zpsp3.0000118\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":10000,\"productNormName\":\"卡灰规格1\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg\",\"productItemId\":1138,\"description\":\"香醇味浓的黑卡灰/dog\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg!sellout\",\"productNum\":\"kh1.0000127\",\"normTypeName\":\"规格\",\"productName\":\"黑卡灰\"},{\"quantity\":100,\"productNormName\":\"卡灰规格3\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1164,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"kh3.0000129\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":100,\"productNormName\":\"卡灰规格5\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1165,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"kh5.0000131\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":12000,\"productNormName\":\"卡灰规格2\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg\",\"productItemId\":1135,\"description\":\"香醇味浓的黑卡灰/dog\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1530080294169.jpeg!sellout\",\"productNum\":\"kh2.0000128\",\"normTypeName\":\"规格\",\"productName\":\"黑卡灰\"},{\"quantity\":94,\"productNormName\":\"卡灰规格4\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130799.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131098.jpeg,http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965131476.jpeg\",\"productItemId\":1167,\"description\":\"卖荔枝了喂，三元一斤十元三斤啦\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1528965130283.jpeg\",\"productNum\":\"kh4.0000130\",\"normTypeName\":\"单位\",\"productName\":\"荔枝\"},{\"quantity\":0,\"productNormName\":\"卡飞规格\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1531208290804.jpeg!sellout!watermark\",\"productItemId\":1096,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1531208290804.jpeg!sellout\",\"productNum\":\"kf.0000132\",\"normTypeName\":\"单位\",\"productName\":\"卡飞\"},{\"quantity\":23,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productItemId\":1129,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productNum\":\"zl.0000217\",\"normTypeName\":\"个\",\"productName\":\"中粮15.16.17\"},{\"quantity\":121,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productItemId\":1128,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productNum\":\"zl.0000215\",\"normTypeName\":\"个\",\"productName\":\"中粮15.16.17\"},{\"quantity\":456,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productItemId\":1130,\"description\":\"1\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1541387594756.jpeg\",\"productNum\":\"zl.0000216\",\"normTypeName\":\"个\",\"productName\":\"中粮15.16.17\"},{\"quantity\":57,\"productNormName\":\"个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1543895153452.jpeg\",\"productItemId\":1143,\"description\":\"海外商品 分销\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1543895153452.jpeg\",\"productNum\":\"haiwai001\",\"normTypeName\":\"个\",\"productName\":\"海外商品 分销\"},{\"quantity\":420,\"productNormName\":\"1个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productItemId\":1144,\"description\":\"海外商品4\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productNum\":\"hwsp004\",\"normTypeName\":\"个\",\"productName\":\"海外商品4\"},{\"quantity\":9997,\"productNormName\":\"1个\",\"price\":1.0,\"largeImageUrl\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productItemId\":1146,\"description\":\"海外商品4\",\"firstImage\":\"http://testyouzhipai.b0.upaiyun.com/uploads/pic/13/1544079626791.jpeg\",\"productNum\":\"hwbxy\",\"normTypeName\":\"个\",\"productName\":\"物料海外\"}],\"status\":\"success\"}";
            resultMap = JSONObject.parseObject(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (resultMap == null) {
            throw new BaseServiceException("查询分销商品的库存和价格失败~");
        }
        //data数组
        List dataList = (List) resultMap.get("data");
        for (Object o : dataList) {
            Map<String, Object> data = JSONObject.parseObject(String.valueOf(o));
            //穿过来的数据封装成supplierGoods对象
            SupplierGoods supplierGoods = packGoods(data);
            Long brandid = supplierGoods.getBrandid();
            SupplierGoods supplierGoodsQuer = new SupplierGoods();
            supplierGoodsQuer.setBrandid(brandid);
            List<SupplierGoods> supplierGoodsQuers = this.supplierGoodsService.queryAll(supplierGoodsQuer);
            if (supplierGoodsQuers == null) {
                queryYzpProductList(params);
            }
            for (SupplierGoods goodsQuer : supplierGoodsQuers) {
                SupplierProduct supplierProduct = new SupplierProduct();
                supplierProduct.setProductId(String.valueOf(goodsQuer.getBrandid()));
                List<SupplierProduct> supplierProducts = this.supplierProductService.queryAll(supplierProduct);
                //查询supplierProduct表对比库存
                for (SupplierProduct product : supplierProducts) {
                    if (product.getInventoryNum() != data.get("quantity")) {
                        SupplierProduct supplierProductUpdate = new SupplierProduct();
                        supplierProductUpdate.setInventoryNum((Integer) data.get("quantity"));
                        supplierProductUpdate.setId(product.getId());
                        //更新库存写入日志
                        this.supplierProductService.modify(supplierProductUpdate, false);
                        //推送商城
                        shopService.pushInventory(Long.valueOf(product.getId()), String.valueOf(data.get("quantity")));
                    }


                    if (data.get("price") != null) {
                        //对比价格
                        String price = String.valueOf(data.get("price"));
                        Integer goodPrice = 0;
                        if (price.contains(".")) {
                            Double i = Double.valueOf(price) * 100;
                            goodPrice = Integer.valueOf(String.valueOf(i).split("\\.")[0]);
                        } else {
                            goodPrice = Integer.valueOf(price) * 100;

                        }
                        // TODO 暂定进价85折
                        Integer inGoodPrice = (goodPrice * 85) / 100;
                        SupplierProductPrice supplierProductPriceQuer = new SupplierProductPrice();
                        supplierProductPriceQuer.setSupplierProductId(product.getId());
                        List<SupplierProductPrice> supplierProductPrices = this.supplierProductPriceService.queryAll(supplierProductPriceQuer);
                        for (SupplierProductPrice supplierProductPrice : supplierProductPrices) {
                            //价格不同
                            if (supplierProductPrice.getCooperateprice() != goodPrice) {
                                SupplierProductPrice supplierProductPriceUpdate = priceCalculation(0, 0, inGoodPrice, goodPrice, 0, supplierProductPrice.getSupplierProductId());
                                supplierProductPriceUpdate.setId(supplierProductPrice.getId());
                                int modify = this.supplierProductPriceService.modify(supplierProductPriceUpdate, false);
                                //推送到商城
                                H5RespModel h5RespModel = shopService.pushH5(ThirdPartyParam.UPDATE_PRO_H5.getName(), new HashMap<String, Object>() {{
                                    put("skuCode", product.getId());
                                    put("price", supplierProductPrice.getCostPrice());
                                    put("taobaoPrice", supplierProductPrice.getSaleprice());
                                    put("commercePrice", supplierProductPrice.getLeve1Price());
                                    put("elitesPrice", supplierProductPrice.getLeve2Price());
                                    put("crownPrice", supplierProductPrice.getLeve3Price());
                                    put("supremacyPrice", supplierProductPrice.getLeve4Price());
                                    put("cost", supplierProductPrice.getUnionCostPrice());
                                    put("parentCost", supplierProductPrice.getCostPrice());
                                }}, LogType.PRO_PRICE_AND_NUM_YZP_PUSH_SHOP);

                            }


                        }
                    }
                }
            }


        }

        return true;
    }
}
